#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,t,k;
int a[100005],b[100005];
int c[100005];
int f[31];
char d[31][31];
ll ans;
vector<int> v[2];
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    f[0]=1;
    for(int i=1;i<31;i++)  f[i]=f[i-1]*2;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)  scanf("%d",&a[i]);
    for(int i=1;i<=m;i++)  scanf("%d",&c[i]);
    for(int i=1;;i++){
        if(f[i]>n){
            k=i-1;
            break;
        }
    }
    for(int i=1;i<=k;i++)  scanf("%s",d[i]+1);
    scanf("%d",&t);
    while(t--){
        for(int i=0;i<4;i++)  scanf("%d",&b[i]);
        for(int i=1;i<=n;i++)  a[i]^=b[i%4];
        for(int i=1;i<=m;i++){
            if(c[i]-(c[i]&(-c[i]))!=0){
                printf("1\n");
                break;
            }
            ll rt=i;
            int cnt=c[i],r=1;
            for(int j=1;j<=c[i];j++)  v[0].push_back(j);
            while(cnt>1){
                int l=v[(r%2+1)%2].size();
                for(int o=0;o<l;o+=2){
                    int p1=v[(r%2+1)%2][o],p2=v[(r%2+1)%2][o+1];
                    if(d[r][o/2+1]=='0'){
                        if(a[p1]>=r)  v[r%2].push_back(p1);
                        else  v[r%2].push_back(p2);
                    }
                    else{
                        if(a[p2]>=r)  v[r%2].push_back(p2);
                        else  v[r%2].push_back(p1);
                    }
                }
                cnt/=2;
                r++;
            }
            if(v[0].size())  rt*=(ll)v[0][0];
            else  rt*=(ll)v[1][0];
            if(i==1)  ans=rt;
            else  ans^=rt;
        }
        printf("%lld\n",ans);
        for(int i=1;i<=n;i++)  a[i]^=b[i%4];
    }
    return 0;
}
